<?php require_once('Connections/local.php'); ?>
<?php

session_start();
if (!isset($_SESSION['MM_COD_USUARIO']))
    header("location:login.php");
	if (($_SESSION['MM_ROLE'] == 'socio'))
    header("location:pagina.php");
	
	?>
<?php
	
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO usuarios (NOMBRE, APELLIDO, DNI, Tlf, Correo, Domicilio, Municipio, FNacimiento, ROL, Contrasena) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, password(%s))",
                       GetSQLValueString($_POST['NOMBRE'], "text"),
                       GetSQLValueString($_POST['APELLIDO'], "text"),
                       GetSQLValueString($_POST['DNI'], "text"),
                       GetSQLValueString($_POST['Tlf'], "int"),
                       GetSQLValueString($_POST['Correo'], "text"),
                       GetSQLValueString($_POST['Domicilio'], "text"),
                       GetSQLValueString($_POST['Municipio'], "text"),
                       GetSQLValueString($_POST['FNacimiento'], "date"),
					   GetSQLValueString($_POST['ROL'], "text"),
                       GetSQLValueString($_POST['Contrasena'], "text"));

  mysql_select_db($database_local, $local);
  $Result1 = mysql_query($insertSQL, $local) or die(mysql_error());

  $insertGoTo = "consultausuario.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<html>
<body>

<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">

<form method="post" name="form1" action="<?php echo $editFormAction;?>"  
<div id="stylized" class="table" >
  <table align="center">
    <tr>
      <td >NOMBRE:</td>
      <td><span id="sprytextfield1">
        <input type="text" name="NOMBRE" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td >APELLIDO:</td>
      <td><span id="sprytextfield2">
        <input type="text" name="APELLIDO" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td >DNI:</td>
      <td><span id="sprytextfield3">
        <input type="text" name="DNI" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td>Tlf:</td>
      <td><span id="sprytextfield4">
       <input type="text" name="Tlf" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td>Correo:</td>
      <td><span id="sprytextfield5">
      <input type="text" name="Correo" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span><span class="textfieldInvalidFormatMsg">Formato no válido.</span></span></td>
    </tr>
    <tr>
      <td>Domicilio:</td>
      <td><span id="sprytextfield6">
        <input type="text" name="Domicilio" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td>Municipio:</td>
      <td><span id="sprytextfield7">
      <input type="text" name="Municipio" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td>FNacimiento(A/M/D):</td>
      <td><span id="sprytextfield8">
       <input type="text" name="FNacimiento" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
	
	<select name="ROL">
    <option value="administrador">Administrador</option>
	<option value="socio">Socio</option>
      </select>
	
    <tr>
      <td>Contrasena:</td>
      <td><span id="sprytextfield9">
      <input type="password" name="Contrasena" value="" size="32">
      <span class="textfieldRequiredMsg">Se necesita un valor.</span></span></td>
    </tr>
    <tr>
      <td><input type="submit" value="Insertar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</div>
  </form>

<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");
var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5", "email");
var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6");
var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7");
var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8");
var sprytextfield9 = new Spry.Widget.ValidationTextField("sprytextfield9");
</script>
</body>
</html>